package com.csproj.system.repository.impl;

import com.csproj.system.domain.SysLogs;
import com.csproj.system.repository.custom.SysLogsRepositoryCustom;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;

public class SysLogsRepositoryImpl  implements SysLogsRepositoryCustom {

    @PersistenceContext
    private EntityManager em;

    public EntityManager getEm() {
        return em;
    }

    @Override
    public Page<SysLogs> findPageList(Map<String, Object> queryParams, Pageable pageable) {
        String hsql = " select sl from SysLogs sl left join UserInfo ui on ui.id = sl.userInfoId  where 1=1  ";
        Map<String, Object> hsqlMap = new HashMap<>();
//        String keyWord = (String) queryParams.get("keyWord");
        String name = (String) queryParams.get("name");
        String roleId = (String) queryParams.get("roleId");
        ZonedDateTime startTime = (ZonedDateTime) queryParams.get("startTime");
        ZonedDateTime endTime = (ZonedDateTime) queryParams.get("endTime");
        if(StringUtils.isNotBlank(name)){
            hsql += " and ui.name like :name ";
            hsqlMap.put("name","%"+name+"%");
        }
//
        if(StringUtils.isNotBlank(roleId)){
            hsql += " and sl.roleInfoIds like :roleId ";
            hsqlMap.put("roleId","%,"+roleId+",%");

        }

        if(startTime!=null){
            hsql += " and sl.creationTime > :startTime ";
            hsqlMap.put("startTime",startTime);
        }

        if(endTime != null ){
            hsql += " and sl.creationTime <:endTime ";
            hsqlMap.put("endTime",endTime);
        }

        hsql +=" order by sl.creationTime desc";
        Page<SysLogs> resultPage = query(hsql,pageable,hsqlMap);
        return resultPage;

    }

}
